package besta.moon.sql.expression.function.system;

import besta.moon.sql.datatype.MutableNumeric;
import besta.moon.sql.expression.function.ExpressionFunction;
import besta.moon.sql.expression.ExpressionArithmetic;
import besta.moon.sql.parser.SQLTokenizer;

public final class FunctionIIF extends ExpressionFunction {

    @Override
    public int getFunction() {
        return SQLTokenizer.IIF;
    }

    @Override
    public boolean isNull() throws Exception {
        if (param1.getBoolean()) {
            return param2.isNull();
        }
        return param3.isNull();
    }

    @Override
    public boolean getBoolean() throws Exception {
        if (param1.getBoolean()) {
            return param2.getBoolean();
        }
        return param3.getBoolean();
    }

    @Override
    public int getInt() throws Exception {
        if (param1.getBoolean()) {
            return param2.getInt();
        }
        return param3.getInt();
    }

    @Override
    public long getLong() throws Exception {
        if (param1.getBoolean()) {
            return param2.getLong();
        }
        return param3.getLong();
    }

    public float getFloat() throws Exception {
        if (param1.getBoolean()) {
            return param2.getFloat();
        }
        return param3.getFloat();
    }

    @Override
    public double getDouble() throws Exception {
        if (param1.getBoolean()) {
            return param2.getDouble();
        }
        return param3.getDouble();
    }

    @Override
    public long getMoney() throws Exception {
        if (param1.getBoolean()) {
            return param2.getMoney();
        }
        return param3.getMoney();
    }

    @Override
    public MutableNumeric getNumeric() throws Exception {
        if (param1.getBoolean()) {
            return param2.getNumeric();
        }
        return param3.getNumeric();
    }

    @Override
    public Object getObject() throws Exception {
        if (param1.getBoolean()) {
            return param2.getObject();
        }
        return param3.getObject();
    }

    @Override
    public String getString() throws Exception {
        if (param1.getBoolean()) {
            return param2.getString();
        }
        return param3.getString();
    }

    @Override
    public final int getDataType() {
        return ExpressionArithmetic.getDataType(param2, param3);
    }

    @Override
    public final int getPrecision() {
        return Math.max(param2.getPrecision(), param3.getPrecision());
    }

    @Override
    public final int getScale() {
        return Math.max(param2.getScale(), param3.getScale());
    }

}
